package com.ishansong.ticket.service.impl;


import com.ishansong.ticket.enums.TicketStatusEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ishansong.ticket.exception.ServiceDaoException;
import com.ishansong.ticket.exception.ServiceException;
import com.ishansong.ticket.model.Ticket;
import com.ishansong.ticket.service.TicketService;
import com.matrixjoy.dal.dao.Dao;
import com.matrixjoy.dal.dao.exception.DaoException;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;


@Component( value= "ticketService" )
public class TicketServiceImpl implements TicketService {

	@Autowired
	private Dao dao;

	@Override
	public Boolean deleteTicket(Long id) throws ServiceDaoException, ServiceException {
		try {
			return (Boolean) dao.delete(Ticket.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public int queryTicketCountByOrderNumber(String orderNumber) throws ServiceDaoException, ServiceException {
		try {
			return dao.count("queryTicketCountByOrderNumber", new Object[]{orderNumber });
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public List<Ticket> queryTicketByOrderNumber(String orderNumber) throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = dao.getIdList("queryTicketByOrderNUmber", new Object[] { orderNumber });
			if (!CollectionUtils.isEmpty(ids)) {
				return dao.getList(Ticket.class, ids);
			} else {
				return Collections.emptyList();
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public List<Ticket> queryTicketPageByStatus(Integer status,int start,int pageSize,String operatorId) throws ServiceDaoException, ServiceException {
		try {
			List<Serializable> ids = null;
			if (status != null) {
				if(StringUtils.isNotBlank(operatorId) && status != TicketStatusEnum.DCL.getStatus()) {
					ids = dao.getIdList("queryTicketPageByStatusAndOperatorId", new Object[] {status,operatorId},(start-1)*pageSize,pageSize);
				} else {
					ids = dao.getIdList("queryTicketPageByStatus", new Object[] {status},(start-1)*pageSize,pageSize);
				}
				if (!CollectionUtils.isEmpty(ids)) {
					return dao.getList(Ticket.class, ids);
				} else {
					return Collections.emptyList();
				}
			} else {
				ids = dao.getIdList("queryTicketPage", new Object[] {},(start-1)*pageSize,pageSize);
				if (!CollectionUtils.isEmpty(ids)) {
					return dao.getList(Ticket.class, ids);
				} else {
					return Collections.emptyList();
				}
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public int queryTicketCountByStatus(Integer status, String operatorId) throws ServiceDaoException, ServiceException {
		try {
			if (status != null) {
				if(StringUtils.isNotBlank(operatorId) && status != TicketStatusEnum.DCL.getStatus()) {
					return dao.count("queryTicketCountByStatusAndOperatorId", new Object[] {status,operatorId});
				} else {
					return dao.count("queryTicketCountByStatus", new Object[] {status});
				}

			} else {
				return dao.count("queryTicketCount", new Object[] {});
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public List<Ticket> queryTicketByMobile(String mobile) throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = dao.getIdList("queryTicketByMobile", new Object[] { mobile },0,5);
			if (!CollectionUtils.isEmpty(ids)) {
				return dao.getList(Ticket.class, ids);
			} else {
				return Collections.emptyList();
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}



	@Override
	public Ticket getTicket(Long id) throws ServiceDaoException, ServiceException {
		try {
			if (null == id) {
				return null;
			}
			return (Ticket) dao.get(Ticket.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public Long saveTicket(Ticket ticket) throws ServiceDaoException, ServiceException {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String formatStr =formatter.format(new Date());
			ticket.setNumber(formatStr);
			return (Long) dao.save(ticket);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public void updateTicket(Ticket ticket) throws ServiceDaoException, ServiceException {
		try {
			dao.update(ticket);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}

	}

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

}
